package com.ruoyi.zongdianpifa.controller;

import java.util.List;

import com.ruoyi.cangku.domain.Cangku;
import com.ruoyi.cangku.service.ICangkuService;
import com.ruoyi.goods.service.IGoodsService;
import com.ruoyi.kucunpandian.domain.Kucunpandian;
import com.ruoyi.kucunpandian.service.IKucunpandianService;
import com.ruoyi.shangpinxinxi.service.IShangpinxinxiService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.zongdianpifa.domain.ZongdianPifa;
import com.ruoyi.zongdianpifa.service.IZongdianPifaService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;

/**
 * 总店批发Controller
 * 
 * @author ruoyi
 * @date 2023-02-06
 */
@Controller
@RequestMapping("/zongdianpifa/pifaTest")
public class ZongdianPifaController extends BaseController
{
    private String prefix = "zongdianpifa/pifaTest";

    @Autowired
    private IZongdianPifaService zongdianPifaService;

    @Autowired
    private IKucunpandianService kucunpandianService;
    @Autowired
    private ICangkuService cangkuService;

    @RequiresPermissions("zongdianpifa:pifaTest:view")
    @GetMapping()
    public String pifaTest()
    {
        return prefix + "/pifaTest";
    }

    /**
     * 查询总店批发列表
     */
    @RequiresPermissions("zongdianpifa:pifaTest:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request, ZongdianPifa zongdianPifa)
    {
//        startPage();
//        List<ZongdianPifa> list = zongdianPifaService.selectZongdianPifaList(zongdianPifa);
//        return getDataTable(list);
        startPage();
        if (!request.getSession().getAttribute("QuanXian").toString().equals("1")
        && (zongdianPifa.getChukuCangku() == null
        || zongdianPifa.getChukuCangku().equals(""))) {
            List<Cangku> name2cangku = cangkuService.selectCangkuAll();
            ZongdianPifa zongdianPifa1 = new ZongdianPifa();
            for (Cangku c:name2cangku) {
                if (c.getCangkuName().equals(request.getSession().getAttribute("name4user").toString())) {
                    zongdianPifa1.setChukuCangku(c.getCangkuName());
                }
            }
            List<ZongdianPifa> list = zongdianPifaService.selectZongdianPifaList(zongdianPifa1);
            return getDataTable(list);
        }else {
            List<ZongdianPifa> list = zongdianPifaService.selectZongdianPifaList(zongdianPifa);
            return getDataTable(list);
        }
    }

    /**
     * 导出总店批发列表
     */
    @RequiresPermissions("zongdianpifa:pifaTest:export")
    @Log(title = "总店批发", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ZongdianPifa zongdianPifa)
    {
        List<ZongdianPifa> list = zongdianPifaService.selectZongdianPifaList(zongdianPifa);
        ExcelUtil<ZongdianPifa> util = new ExcelUtil<ZongdianPifa>(ZongdianPifa.class);
        return util.exportExcel(list, "总店批发数据");
    }

    /**
     * 新增总店批发
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存总店批发
     */
    @RequiresPermissions("zongdianpifa:pifaTest:add")
    @Log(title = "总店批发", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ZongdianPifa zongdianPifa)
    {
        String numstr = zongdianPifaService.selectZongdianPifaIdByLastOne();
        int num;
        if (numstr == null) {
            num = 1;
        }else {
            num = Integer.parseInt(numstr)+1;
        }

        String b = "";
        if (String.valueOf(num).length() < 8) {
            int a = 8-String.valueOf(num).length();
            for (int j = 0; j < a; j++) {
                b="0" + b;
            }
        }
        zongdianPifa.setChukuDanhao(b);

        Kucunpandian kucunpandian = kucunpandianService.selectCangkupandianByGoodIdAndCangKuNameAndGoodName(
                zongdianPifa.getGoodId(),zongdianPifa.getChukuCangku(), zongdianPifa.getGoodName()
        );
        if (kucunpandian == null) {
            return toAjax(-1);
        }
        Long kucunNew = kucunpandian.getKuncun()- zongdianPifa.getShuliang().longValue();
        kucunpandian.setKuncun(kucunNew);
        kucunpandianService.updateKucunpandian(kucunpandian);

        Double zongJinEr = zongdianPifa.getChukuDanjia() * zongdianPifa.getShuliang();
        zongdianPifa.setJiner(zongJinEr);

        return toAjax(zongdianPifaService.insertZongdianPifa(zongdianPifa));
    }

    /**
     * 修改总店批发
     */
    @RequiresPermissions("zongdianpifa:pifaTest:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ZongdianPifa zongdianPifa = zongdianPifaService.selectZongdianPifaById(id);
        mmap.put("zongdianPifa", zongdianPifa);
        return prefix + "/edit";
    }

    /**
     * 修改保存总店批发
     */
    @RequiresPermissions("zongdianpifa:pifaTest:edit")
    @Log(title = "总店批发", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ZongdianPifa zongdianPifa)
    {
        Kucunpandian kucunpandian = kucunpandianService.selectCangkupandianByGoodIdAndCangKuNameAndGoodName(
                zongdianPifa.getGoodId(), zongdianPifa.getChukuCangku(), zongdianPifa.getGoodName()
        );
        ZongdianPifa zongdianPifa1 = zongdianPifaService.selectZongdianPifaBychukuDanhao(zongdianPifa.getChukuDanhao());
        Long kucunNew = kucunpandian.getKuncun()
                - zongdianPifa.getShuliang().longValue()
                + zongdianPifa1.getShuliang().longValue();
        kucunpandianService.updateKucunpandian(kucunpandian);

        Double zongJinEr = zongdianPifa.getShuliang() * zongdianPifa.getChukuDanjia();
        zongdianPifa.setJiner(zongJinEr);

        return toAjax(zongdianPifaService.updateZongdianPifa(zongdianPifa));
    }

    /**
     * 删除总店批发
     */
    @RequiresPermissions("zongdianpifa:pifaTest:remove")
    @Log(title = "总店批发", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        Long id = Long.valueOf(ids);
        ZongdianPifa xiaoshouChuku = zongdianPifaService.selectZongdianPifaById(id);
        Kucunpandian cangkupandian = new Kucunpandian();

        cangkupandian.setCangkuName(xiaoshouChuku.getChukuCangku());
        cangkupandian.setGoodId(xiaoshouChuku.getGoodId());
        cangkupandian.setGoodName(xiaoshouChuku.getGoodName());
        cangkupandian.setGoodSpecification(xiaoshouChuku.getGoodGuige());

        Kucunpandian kucunpandian = kucunpandianService.selectCangkupandianByGoodIdAndCangKuNameAndGoodName(xiaoshouChuku.getGoodId(), xiaoshouChuku.getChukuCangku(),xiaoshouChuku.getGoodName());
        cangkupandian.setId(kucunpandian.getId());
        cangkupandian.setShangpinLeibie(xiaoshouChuku.getGoodGuige());
        cangkupandian.setKuncun(kucunpandian.getKuncun() + xiaoshouChuku.getShuliang().longValue());

        kucunpandianService.updateKucunpandian(cangkupandian);

        return toAjax(zongdianPifaService.deleteZongdianPifaByIds(ids));
    }
}
